import { Component, OnInit,Output,Input,EventEmitter } from '@angular/core';
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { NzModalRef, NzFormatEmitEvent, NzModalService, NzTreeComponent, NzTreeNodeOptions, NzMessageService } from 'ng-zorro-antd';
import { PagesService } from '../../services/pages.service';
import { UtilService } from '../../services/util.service';
import { RequestService } from '../../services/request.service';
declare var layui: any;
declare var layer: any;
declare var $: any;

@Component({
  selector: 'app-prod',
  templateUrl: './prod.component.html',
  styleUrls: ['./prod.component.scss']
})
export class ProdComponent implements OnInit {

  constructor(public router: Router, private http: HttpClient, public interfaceService: PagesService, public utilService: UtilService,public request:RequestService) {
    
  }

  @Output()  private  outer=new EventEmitter();
  @Input() puc_msg:any; 

  field1 = []     //table表格1-主表列字段集合
  data1 = {}      //table表格1-主表对象数据集合
  data1_dis = {}  //table表格1-主表对象禁用集合
  data1_sel = []  //table表格1-主表下拉框数据集合
  data1_att = []  //table表格1-主表自定义属性数据集合
  /**
   * 表格字段动态列集合生成
   */
  filed_tablelist() {
    this.interfaceService.postPage("/erp/common/fields/getfieldsbytype", { "type": "purchase.DtlProdVo" }).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      if (data.code == 200) {
        this.field1 = this.utilService.filed_list4(data.object);
        for (var i = 0; i < data.object.length; i++) {
          this.data1[data.object[i]["fieldValue"]] = ""
          this.data1_dis[data.object[i]["fieldValue"]] = true
        }
      }
    }
    });

  }


  ngOnInit() {
    this.request.getProdCat1().subscribe((data)=>{//商品树一级分类
      this.nodes=data;
      this.request.getBrand().subscribe((data)=>{//品牌名称
        this.brandId=data.object.list;
        this.request.getColor().subscribe((data)=>{//所有颜色
          this.colorName_list=data.object.list;
          this.request.getSpec().subscribe((data)=>{//所有规格
            this.specId_list0=data.object;
          });
        });
      });
    })
    this.filed_tablelist()
  }

  nodes = [];
  brandId=[];
  colorName_list=[];
  specId_list0=[];

  //单独写：分类树、品牌、颜色、规格
  /*
  nodes = JSON.parse(localStorage.getItem("shu1"));
  brandId=JSON.parse(localStorage.getItem("pinpai"));
  colorName_list=JSON.parse(localStorage.getItem("yanse"));
  specId_list0=JSON.parse(localStorage.getItem("guige"));

   brandId;
  nodes = [];
  tree1(): void {//商品分类树一级
    var getProdCat1 = this.request.getProdCat1();
    getProdCat1.subscribe((data) => {           //商品分类
      this.nodes = data;
    })
    var getBrand = this.request.getBrand();			//品牌名称
    getBrand.subscribe((data) => {
      this.brandId = data.object.list;
    })
  } */
  ddata = [];
  tree2(e: Required<NzFormatEmitEvent>): void {
    if (e.node.getChildren().length === 0 && e.node.isExpanded) {
      if (e.node.origin.key) {
        this.loadNode("/erp/product/prodCat/opetion?prodCatId=" + e.node.origin.key).then((data: any) => {
          e.node.addChildren(data.object.list);
        });
      } else {
        e.node.addChildren(this.ddata);
      }
    }
  }

  xiaNodws = [];
  loadNode(xia): Promise<any[]> {
    this.interfaceService.getPages(xia, 1, 1000).subscribe((data: any) => {
      this.xiaNodws = data;
    });
    return new Promise(resolve => {
      setTimeout(() => resolve(this.xiaNodws),
        1000);
    });
  }

  //商品选择
  prodStatus_list=JSON.parse(localStorage.getItem("prodStatus_list"))	//商品状态
  popup01_url='';
  popup01_para
  popup01 = false;
  popup01_show(url,para?): void {//第二参数undefined代表查询类型，否则代表验证类型
    this.popup01_url=url
    this.popup01_para=para;
    if (typeof(this.popup01_para) == "undefined"){
      this.popup01 = true;
      this.popup01_table();
    }else{
      this.interfaceService.getPage(this.popup01_url+"&prodClsCode="+this.popup01_para).subscribe((data1: any) => {
        if (this.interfaceService.codeValidation(data1)) {
        if (data1.code != 200) {
          layer.msg(data1.message);
        }
      }
        this.outer.emit(data1.object); 
      });
    } 
  }
  popup01_hide(): void {
    this.popup01 = false;
    this.popup01_url='';
    this.popup01_ss1 = '';
    this.popup01_ss2 = '';
    this.popup01_ss3 = '';
    this.popup01_ss4 = '';
    this.popup01_ss5 = '';
    this.popup01_ss6 = '';
  }

  popup01_ss1 = '';
  popup01_ss2 = '';
  popup01_ss3 = '';
  popup01_ss4 = '';
  popup01_ss5 = '';
  popup01_ss6 = '';
  popup01_table() {
    this.popup01_listTable(this.popup01_url+"&prodCode=" + this.popup01_ss1 + "&prodName=" + this.popup01_ss2 + "&prodCatId=" + this.popup01_ss3 + "&inputCode=" + this.popup01_ss4 + "&brandId=" + this.popup01_ss5 + "&prodStatus=" + this.popup01_ss6);
  }
  indexTableId = 'popup01_tblist_prod'
  popup01_listTable(url): void {
    setTimeout(() => {
      layui.use(['table'], (obj) => {
        var  table = layui.table
        var tab = this.utilService.getTable(url, "#" + this.indexTableId, this.field1)
        tab["title"] = '明细弹窗数据表'
        table.render(tab);
        table.on('rowDouble(popup01_tblist_prod)', (obj) => {        
          this.popup01_hide();
          this.outer.emit(obj.data)      
        });

      });

    }, 50);

  }

}
